import { createSlice,createAsyncThunk,PayloadAction } from "@reduxjs/toolkit";
import * as API from '@/utils/http'
import { store } from "./index";
let initialState={
  CWtitle:[],
  customerlist:[]
}
type initialStateTS={
  customerlist:Array<customerlist>
}
interface customerlist {
  "id":number,
    "title":string,
    "state":boolean,
    "CustomerStarRating":number,
    "contacts":string,
    "phone":number,
    "endTime":number,
    "time":number
}
type StateTs={
  customerlist:object,
}
const crmslice = createSlice({
  name: "crmslice",
  initialState,
  reducers: {
    t_getCw(state, action){//获取财务列表
      state.CWtitle=action.payload;
    },
    t_addCW(state, action){//添加财务
      state.CWtitle.push(action.payload);
    },
    t_editCW(state, action){//回显
      let index=state.CWtitle.findIndex(item=>{
        return item.id==action.payload.id
      })
      state.CWtitle[index]=action.payload;
    },
    t_deleteCW(state, action){//删除财务
    state.CWtitle=state.CWtitle.filter(item=>{
      return item.id!=action.payload
    })
    },
    t_getCustomer(state:initialStateTS,action:PayloadAction<Array<customerlist>>){//获取首页客户列表数据
      state.customerlist=action.payload
    },
    t_addCustomer(state:initialStateTS, action:PayloadAction<customerlist>){//添加首页客户列表
      state.customerlist.push(action.payload);
    },
    t_editCustomer(state:initialStateTS, action:PayloadAction<customerlist>){//回显首页客户列表
      let index=state.customerlist.findIndex(item=>{
        return item.id==action.payload.id
      })
      state.customerlist[index]=action.payload;
    },
    t_delCustomer(state:initialStateTS, action:PayloadAction<number>){//删除首页客户列表
    state.customerlist=state.customerlist.filter(item=>{
      return item.id!=action.payload
    })
    },
  },
});

export const comfn=crmslice.actions;
export default crmslice.reducer;
export let async_getCW=createAsyncThunk("async_getCW",//异步获取财务列表
  async(_,store)=>{
    let res=await API.http_title()
    store.dispatch(comfn.t_getCw(res.data))
  }
)
export let async_addCW=createAsyncThunk("async_addCW",//异步添加财务列表
  async(obj,store)=>{
    let res=await API.add_title(obj)
    store.dispatch(comfn.t_addCW(res.data))
  }
)
export let async_editCW=createAsyncThunk("async_editCW",//异步修改财务列表
  async(obj,store)=>{
    let res=await API.edit_title(obj)
    store.dispatch(comfn.t_editCW(res.data))
  }
)
export let async_delCW=createAsyncThunk("async_delCW",//异步删除财务列表
  async(id,store)=>{
    await API.del_title(id)
    store.dispatch(comfn.t_deleteCW(id))
  }
)
export let async_getcustomerlist=createAsyncThunk("async_getcustomerlist",//异步获取首页首页列表
  async(_,store)=>{
    const res=await API.http_getcustomerlsit()
    store.dispatch(comfn.t_getCustomer(res.data))
  })
  export let async_addcustomerlist=createAsyncThunk("async_addcustomerlist",//异步添加首页列表
    async(obj,store)=>{
      let res=await API.http_addcustomerlist(obj as any)
      store.dispatch(comfn.t_addCustomer(res.data))
    }
  )
  export let async_editcustomerlist=createAsyncThunk("async_editcustomerlist",//异步修改首页列表
    async(obj,store)=>{
      let res=await API.http_editcustomerlist(obj as any)
      store.dispatch(comfn.t_editCustomer(res.data))
    }
  )
  export let async_delcustomerlist=createAsyncThunk("async_delcustomerlist",//异步删除首页列表
    async(id,store)=>{
      await API.http_delcustomerlist(id as any)
      store.dispatch(comfn.t_delCustomer(id as any))
    }
  )
export let async_searchcustomerlist=createAsyncThunk("async_searchcustomerlist",
  async(obj,store)=>{
    const res= await API.http_searchcustomerlist(obj as any)
    store.dispatch(comfn.t_getCustomer(res.data))
  }
)